package com.aranandroid.customview.progress

import android.content.Context
import kotlin.jvm.JvmOverloads
import android.graphics.*
import android.graphics.Paint.Style
import com.aranandroid.customview.R
import android.util.AttributeSet
import com.blankj.utilcode.util.ImageUtils
import android.util.TypedValue
import android.view.View
import android.widget.Switch

/**
 * Created by Chen on 2017/6/4.
 * 圆形数字进度条
 */
class RoundProgres2View @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    //进度
    private var mProgress = 0

    //半径
    private var mRadius = DEFAULT_RADIUS

    //进度条的高度
    private var mProgressHeight = 0

    //文字的大小
    private var mTextSize = 0

    // 字体加粗斜体
    private var mTextStyle = 0

    //文字颜色
    private var mTextColor = 0

    //进度条的颜色
    private var mWaveColor = 0

    //圆形边框的颜色
    private var mBorderColor = 0

    //圆形边框的宽度
    private var borderWidth = 0

    //用于裁剪的Path
    private var mCirclePath: Path? = null

    // 画圆的画笔
    private var mCirclePaint: Paint? = null

    // 画文字的笔
    private var mTextPaint: Paint? = null

    // 画进度条的笔
    private var mProgressPaint: Paint? = null

    // 文字的区域
    private var mTextRect: Rect? = null

    private fun getAttrs(attrs: AttributeSet?) {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.WaveProgres2View)
        mRadius = ta.getDimensionPixelSize(R.styleable.WaveProgres2View_radius, DEFAULT_RADIUS)
        mProgressHeight = mRadius * 2
        mTextColor = ta.getColor(R.styleable.WaveProgres2View_textColor, Color.BLACK)
        mBorderColor = ta.getColor(R.styleable.WaveProgres2View_borderColor, Color.TRANSPARENT)
        mWaveColor = ta.getColor(R.styleable.WaveProgres2View_waveColor, Color.BLUE)
        borderWidth = ta.getDimensionPixelOffset(
            R.styleable.WaveProgres2View_borderWidth, dp2px(
                DEFAULT_BORDER_WIDTH
            )
        )
        mTextSize = ta.getDimensionPixelSize(
            R.styleable.WaveProgres2View_textSize, sp2px(
                DEFAULT_TEXT_SIZE
            )
        )

        mTextStyle = ta.getInt(
            R.styleable.WaveProgres2View_textStyle, 0
        )

        mProgress = ta.getInteger(R.styleable.WaveProgres2View_progress, 0)
        ta.recycle()
    }

    private fun initPath() {
        mCirclePath = Path()
        mCirclePath!!.addCircle(
            mRadius.toFloat(),
            mRadius.toFloat(),
            (mRadius - borderWidth).toFloat(),
            Path.Direction.CCW
        )
    }

    private fun initPaint() {
        mProgressPaint = Paint()
        mProgressPaint!!.color = mWaveColor
        mProgressPaint!!.style = Paint.Style.STROKE
        mProgressPaint!!.isAntiAlias = true
        mProgressPaint!!.strokeWidth = borderWidth.toFloat()
        mCirclePaint = Paint()
        mCirclePaint!!.style = Paint.Style.STROKE // 空心画笔
        mCirclePaint!!.color = mBorderColor
        mCirclePaint!!.isAntiAlias = true
        mCirclePaint!!.strokeWidth = borderWidth.toFloat()
        mTextPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mTextPaint!!.color = mTextColor
        mTextPaint!!.textSize = mTextSize.toFloat()
        var font = Typeface.create(Typeface.SANS_SERIF,  mTextStyle)
        mTextPaint!!.setTypeface(font)
        mTextRect = Rect()

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //圆形的进度条，正好是正方形的内切圆(这边暂时没有考虑padding的影响)
        setMeasuredDimension(mProgressHeight, mProgressHeight)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        //画圆
        canvas.drawCircle(
            mRadius.toFloat(),
            mRadius.toFloat(),
            (mRadius - borderWidth).toFloat(),
            mCirclePaint!!
        )
        val x = borderWidth.toFloat()
        val y = borderWidth.toFloat()
        val oval = RectF(
            x, y,
            width - x, height - y
        )
        canvas.drawArc(oval, -90f,  - (360 * mProgress / 100).toFloat(), false, mProgressPaint!!)
        mTextPaint!!.getTextBounds(progress.toString(), 0, progress.toString().length, mTextRect)
        canvas.drawText(
            progress.toString(), (mRadius - mTextRect!!.width() / 2).toFloat(), (
                    mRadius + mTextRect!!.height() / 2).toFloat(), mTextPaint!!
        )

    }



    /**
     * 设置字体的颜色
     *
     * @param color
     */
    fun setTextColor(color: Int) {
        mTextPaint!!.color = color
    }

    /**
     * 设置进度的颜色
     *
     * @param color
    */
    fun setProgressColor(color: Int) {
    mProgressPaint!!.color = color
    }

    /**
     * 设置
     *
     * @param color
     */
    fun setBorderColor(color: Int) {
        mCirclePaint!!.color = color
    }
    /**
     * 获取当前进度
     *
     * @return
     */
    /**
     * 设置进度
     *
     * @param progress
     */
    var progress: Int
        get() = mProgress
        set(progress) {
            mProgress = progress
            postInvalidate()
        }

    //dp to px
    protected fun dp2px(dpval: Int): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dpval.toFloat(),
            resources.displayMetrics
        ).toInt()
    }

    //sp to px
    protected fun sp2px(dpval: Int): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,
            dpval.toFloat(),
            resources.displayMetrics
        ).toInt()
    }

    companion object {
        /**
         * 默认波长
         */
        private const val DEFAULT_RADIUS = 100

        /**
         * 默认波峰和波谷的高度
         */
        private const val DEFAULT_WAVE_HEIGHT = 5

        /**
         * 默认的最大的进度
         */
        private const val DEFAULT_MAX_PROGRESS = 100

        /**
         * 默认边框宽度
         */
        private const val DEFAULT_BORDER_WIDTH = 2

        /**
         * 默认的进度字体大小
         */
        private const val DEFAULT_TEXT_SIZE = 16
    }

    init {
        getAttrs(attrs)
        initPaint()
        initPath()
    }
}